package com.oauth.server.comoauth2server.domain

import org.springframework.beans.factory.annotation.Autowired
import org.springframework.data.domain.Page
import org.springframework.data.domain.Pageable
import org.springframework.data.domain.PageImpl
import org.springframework.data.mongodb.core.MongoTemplate
import org.springframework.data.mongodb.core.query.Criteria
import org.springframework.data.mongodb.core.query.Query

open class BaseService<T>(private val clazz: Class<T>){
    @Autowired
    lateinit var mongoTemplate: MongoTemplate

    fun findByPage(criteria: Criteria, pageable: Pageable): Page<T> {
        val query= Query.query(criteria).with(pageable)
        val list=mongoTemplate.find(query, clazz)
        val total=mongoTemplate.count(query, clazz)
        return PageImpl(list, pageable, total)
    }

    fun findById(id: String): T? {
        val criteria= Criteria.where("id").`is`(id)
       return mongoTemplate.findOne(Query.query(criteria), clazz)
    }
}